package yss.learn2develop.demoactivity;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import yss.learn2develop.demoservice.YssIntentService;
import yss.learn2develop.demoservice.YssService;


public class LifecycleActivity extends Activity {

    EditText mEditText;
    Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lifecycle);
        Log.i("yss log", "onCreate");

        mEditText = (EditText) findViewById(R.id.life_editText);

        if (savedInstanceState != null) {
            mEditText.setText(savedInstanceState.getString("lifesave"));
        }

        Button _button = (Button) findViewById(R.id.button_startA);
        _button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent _intent = new Intent(LifecycleActivity.this, Second.class);
                startActivity(_intent);
            }
        });

        Button _startButton = (Button)findViewById(R.id.button_start_service);
        _startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mIntent = new Intent(LifecycleActivity.this,YssService.class);
                startService(mIntent);

            }
        });

        Button _stopButton = (Button)findViewById(R.id.button_stop_service);
        _stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(mIntent);
            }
        });

        Button _startIntetntButton = (Button)findViewById(R.id.button_start_intentservice);
        _startIntetntButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mIntent = new Intent(LifecycleActivity.this, YssIntentService.class);
                startService(mIntent);

            }
        });

        Button stopIntentButton = (Button)findViewById(R.id.button_stop_intentservice);
        stopIntentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(mIntent);
            }
        });

        Button _binderButton = (Button)findViewById(R.id.button_binder);
        _binderButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent _intent = new Intent(LifecycleActivity.this,YssService.class);
                bindService( _intent, conn, Service.BIND_AUTO_CREATE);
            }
        });
    }

    //建立连接
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            String message = ((YssService.MyBinder)service).helloWorld();
            Log.i("yss log",message);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.lifecycle, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("yss log", "onStart");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("yss log", "onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("yss log", "onResume");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("yss log", "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("yss log", "onDestroy");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("yss log", "onPause");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //super.onSaveInstanceState(outState);
        outState.putString("lifesave", "yss:" + mEditText.getText());
        Log.i("yss log", "onSaveInstanceState");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.i("yss log", "onConfigurationChanged");

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.i("yss log","横屏模式");

        }else
        {
            Log.i("yss log","竖屏模式");
        }

    }
}
